# -*- coding:utf-8 -*-
# @Time : 2022/6/6 下午1:50 
# @Author : zdy
# @File : UICheckLeck.py
# desc: 检漏
import threading
from time import sleep

from importQT import *
from UI.CheckLeck import Ui_CheckLeck
from Class.Devices import *

class UICheckLeck(QDialog, Ui_CheckLeck):
    sigStatusUpdate = pyqtSignal(str)
    def __init__(self):
        super(UICheckLeck, self).__init__()
        self.setupUi(self)
        self.setWindowFlags(Qt.WindowCloseButtonHint)
        self.setFixedSize(self.width(), self.height())
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.applyResource()
        self.btnStart.clicked.connect(self.btnStart_clicked)
        self.btnStop.clicked.connect(self.btnStop_clicked)
        self.sigStatusUpdate.connect(self.update)
        self.abort = False
        self.result = -1 # 0-不漏气；-1检漏失败；1整体漏气；2 载气净化漏气；3脉冲炉漏气；4 分析气路漏气

    def applyResource(self):
        self.setWindowIcon(getIcon())

    def update(self, s):
        if s == "***":
            self.btnStart.setEnabled(True)
        else:
            self.txtbLeck.append(s)

    def btnStart_clicked(self):
        if not device.monitor.furnaceStatus:
            QMessageBox.warning(self, "提示", "升炉后才可检漏")
            return
        self.clickflag = 1
        device.tcBridgeControl = False
        self.btnStart.setEnabled(False)
        self.txtbLeck.clear()
        self.abort = False
        self.result = 0
        t = threading.Thread(target=self.checkLeck)
        # 后台线程
        t.setDaemon(True)
        t.start()

    def btnStop_clicked(self):
        self.abort = True
        pass

    def checkLeck(self):
        self.sigStatusUpdate.emit("关闭桥流")
        device.controlTCBridge(False)
        sleep(15)
        self.sigStatusUpdate.emit("开始整体检漏")
        device.checkLeck(0)
        self.checkPressure(1,20)
        if self.result >0:
            self.sigStatusUpdate.emit("整体检漏漏气，开始分段检漏")
            self.sigStatusUpdate.emit("第一段：载气净化")
            device.checkLeck(1)
            self.checkPressure(2)
            if self.result == 0:
                self.sigStatusUpdate.emit("第二段：脉冲炉")
                device.checkLeck(2)
                self.checkPressure(3)
                if self.result == 0:
                    self.result = 4
        if self.result == -1:
            self.sigStatusUpdate.emit("检漏失败")
        if self.result == 0:
            self.sigStatusUpdate.emit("检漏通过")
        if self.result == 1:
            self.sigStatusUpdate.emit("整体检漏漏气")
        if self.result == 2:
            self.sigStatusUpdate.emit("第一段载气净化漏气")
        if self.result == 3:
            self.sigStatusUpdate.emit("第二段脉冲炉漏气")
        if self.result == 4:
            self.sigStatusUpdate.emit("第三段分析气路漏气")
        self.sigStatusUpdate.emit("***")
        device.reset()

    def checkPressure(self, num, time =8):
        """等待5s，关闭载气，等待time s 记录初始压力，等待检漏时长，记录压力，判断是否漏气"""
        self.sigStatusUpdate.emit("等待5s")
        count = 0
        while not self.abort:
            sleep(0.1)
            count +=1
            if count > 50:
                count = 0
                break
        if self.abort:
            self.result = -1
            return
        device.controlCarrier(False)
        self.sigStatusUpdate.emit("等待"+str(time)+"s")
        count = 0
        while not self.abort:
            sleep(0.1)
            count += 1
            if count > time*10:
                count = 0
                break
        if self.abort:
            self.result = -1
            return
        gas_init = device.monitor.carrierpressure
        self.sigStatusUpdate.emit("初始压力：：%.3f" % gas_init)
        if gas_init<0:
            self.result = num
            return
        checkTime = 30 *2
        count = 0
        while not self.abort:
            sleep(0.5)
            count += 1
            if count > checkTime:
                count = 0
                break
        if self.abort:
            self.result = -1
            return
        gas_second = device.monitor.carrierpressure
        self.sigStatusUpdate.emit("当前压力：：%.3f" % gas_second)
        if gas_second<0:
            self.result= num
            return
        if abs(gas_init - gas_second) > 0.01:
            self.result = num
        else:
            self.result = 0

    def closeEvent(self, event):
        device.tcBridgeControl = True



